<template>
    <div class="show-cards-box" ref="containerRef">
        <div class="nav-buttons">
            <button v-for="(item, index) in parsedData" :key="index" @click="jumpToSlide(index)" class="nav-button">
                镜头#{{ item.stageNo }}
            </button>
        </div>
        <div class="swiper-container">
            <div class="swiper">
                <div class="swiper-wrapper">
                    <div v-for="item in parsedData" :key="item.stageNo" class="swiper-slide card">
                        <div class="card-header">
                            <span class="stage-no">镜头#{{ item.stageNo }}</span>
                            <span class="movie-desc">{{ item.stageMovie }}</span>
                        </div>

                        <div class="card-body">
                            <div class="section ai-tip">
                                <h3>🎨 AI绘画提示<span class="copy-btn"
                                        :data-clipboard-target="'#ai-tip-' + item.stageNo">copy</span></h3>
                                <pre :id="'ai-tip-' + item.stageNo">{{ genAiDrawTip(item) }}</pre>
                            </div>
                            <div class="section text">
                                <h3>💬 台词<span class="copy-btn"
                                        :data-clipboard-target="'#text-' + item.stageNo">copy</span></h3>
                                <p :id="'text-' + item.stageNo">{{ item.text }}</p>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="swiper-button-prev"></div>
                <div class="swiper-button-next"></div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import Swiper from 'swiper'
import { Navigation } from 'swiper/modules'
import 'swiper/css'
import ClipboardJS from 'clipboard'

const props = defineProps({
    parsedData: {
        type: Array,
        required: true
    }
})

const swiperInstance = ref(null);
const containerRef = ref(null);

onMounted(() => {
    swiperInstance.value = new Swiper('.swiper', {
        modules: [Navigation],
        slidesPerView: 2.5,
        spaceBetween: 30,
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        paddingRight: '15%',
        slidesOffsetAfter: 550,
    })
    initClipboard()
})

const initClipboard = () => {
    const clipboard = new ClipboardJS('.copy-btn', {
        text: (trigger) => {
            const target = document.querySelector(trigger.dataset.clipboardTarget)
            return target?.innerText || ''
        }
    })

    clipboard.on('success', (e) => {
        showTooltip(e.trigger, '复制成功！')
        e.clearSelection()
    })

    clipboard.on('error', (e) => {
        showTooltip(e.trigger, '复制失败，请手动选择复制')
    })

    return clipboard
}

const showTooltip = (element, msg) => {
    if (!containerRef.value) return;
    const tip = document.createElement('div')
    tip.className = 'copy-tip'
    tip.textContent = msg

    tip.style.left = `${window.innerWidth / 2 - tip.offsetWidth / 2}px`
    tip.style.top = `10px`

    containerRef.value.appendChild(tip)
    setTimeout(() => tip.remove(), 1500)
}

const jumpToSlide = (index) => {
    swiperInstance.value.slideTo(index);
};

const genAiDrawTip = (item) => {
    return "【镜头描述】：" + item.stageMovie +
        " \n【场景描述】：" + item.aiDrawTip.sceneDescr + (item.aiDrawTip.sceneDescrSuffix || '') +
        " \n【人物细节】：" + item.aiDrawTip.personDetail + (item.aiDrawTip.personDetailSuffix || '') +
        " \n【情感氛围】：" + item.aiDrawTip.emotionalAtmosphere + (item.aiDrawTip.emotionalAtmosphereSuffix || '');
}
</script>

<style>
.show-cards-box {
    width: 100%;
    user-select: none;
}

.nav-buttons {
    display: flex;
    justify-content: left;
    gap: 12px;
}

.nav-button {
    padding: 8px 16px;
    border: 2px solid #2196F3;
    border-radius: 20px;
    background: white;
    color: #2196F3;
    cursor: pointer;
    transition: all 0.3s;
}

.nav-button:hover {
    background: #2196F3;
    color: white;
}

.nav-button:active {
    transform: scale(0.95);
}

.swiper-container {
    width: 100%;
}

.swiper {
    width: 100%;
    padding: 10px;
}

.swiper-slide {
    width: 100%;
}

.card {
    background: white;
    border-radius: 12px;
    box-shadow: 0 3px 6px rgba(0, 0, 0, 0.1);
    padding: 1.5rem;
    transition: all 0.3s;
}

.card-header {
    border-bottom: 2px solid #eee;
    padding-bottom: 1rem;
    margin-bottom: 1rem;
}

.stage-no {
    font-size: 25px;
    font-weight: bold;
    color: #2196F3;
    margin-right: 1rem;
}

.movie-desc {
    color: #666;
    font-size: 0.9em;
}

.section {
    margin: 1.5rem 0;
    padding: 1rem;
    border-radius: 8px;
}

.section h3 {
    margin-top: 0;
    color: #333;
    font-size: 1.1em;
}

.ai-tip {
    background: #e3f2fd;
    border: 1px solid #bbdefb;
}

.ai-tip pre {
    white-space: pre-wrap;
    font-family: monospace;
    margin: 0;
}

.text {
    background: #f0f4c3;
    border: 1px solid #e6ee9c;
}

.swiper-button-prev,
.swiper-button-next {
    padding: 10px;
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 30px;
    height: 30px;
    background-color: rgba(255, 255, 255, 0.9);
    cursor: pointer;
    z-index: 100;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    transition: all 0.3s;
}

.swiper-button-prev {
    left: 10px;
}

.swiper-button-next {
    right: 10px;
}

.swiper-button-prev:hover,
.swiper-button-next:hover {
    background-color: rgba(255, 255, 255, 1);
    transform: translateY(-50%) scale(1.1);
}

.copy-btn {
    margin-left: 10px;
    cursor: pointer;
    padding: 4px 12px;
    border-radius: 8px;
    background: linear-gradient(145deg, #42a5f5, #1e88e5);
    color: white;
    font-size: 12px;
    font-weight: 500;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.copy-btn:hover {
    background: linear-gradient(145deg, #1e88e5, #1565c0);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
    transform: translateY(-1px);
}

.swiper-button-prev {
    padding: 10px;
    position: absolute;
    top: 50%;
    left: 10px;
    transform: translateY(-50%);
    width: 30px;
    height: 30px;
    background-color: rgba(255, 255, 255, 0.9);
    cursor: pointer;
    z-index: 100;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    transition: all 0.3s;
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 27 44'%3E%3Cpath d='M0 22L22 0l2.1 2.1L4.2 22l19.9 19.9L22 44 0 22z' fill='%23007aff'/%3E%3C/svg%3E");
    background-position: center;
    background-repeat: no-repeat;
    background-size: 17px 34px;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.show-cards-box:hover .swiper-button-prev,
.show-cards-box:hover .swiper-button-next {
    opacity: 1;
    visibility: visible;
}

.swiper-button-prev:hover {
    background-color: rgba(255, 255, 255, 1);
    transform: translateY(-50%) scale(1.1);
}

.swiper-button-next {
    padding: 10px;
    position: absolute;
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
    width: 30px;
    height: 30px;
    background-color: rgba(255, 255, 255, 0.9);
    cursor: pointer;
    z-index: 100;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    transition: all 0.3s;
    background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 27 44'%3E%3Cpath d='M27 22L5 44l-2.1-2.1L22.8 22 2.9 2.1 5 0l22 22z' fill='%23007aff'/%3E%3C/svg%3E");
    background-position: center;
    background-repeat: no-repeat;
    background-size: 17px 34px;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
}

.swiper-button-next:hover {
    background-color: rgba(255, 255, 255, 1);
    transform: translateY(-50%) scale(1.1);
}

.copy-tip {
    position: fixed;
    background: linear-gradient(145deg, rgba(176, 233, 205, 0.9), rgba(144, 238, 144, 0.95));
    color: rgb(0, 112, 15);
    padding: 8px 16px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    box-shadow: 0 4px 12px rgba(0, 112, 15, 0.3);
    z-index: 999;
    animation: floatIn 0.3s ease-out, fadeInOut 1.5s 0.3s;
}

@keyframes floatIn {
    0% {
        transform: translateY(20px);
        opacity: 0;
    }

    100% {
        transform: translateY(0);
        opacity: 1;
    }
}
</style>